package com.hl.springboothome1.controller;

import com.github.pagehelper.PageInfo;
import com.hl.springboothome1.entity.Article;
import com.hl.springboothome1.entity.ArticleType;
import com.hl.springboothome1.mapper.ArticleTypeMapper;
import com.hl.springboothome1.service.ArticleService;
import com.hl.springboothome1.service.ArticleTypeService;
import com.hl.springboothome1.service.Impl.ArticleServiceImpl;
import com.hl.springboothome1.service.Impl.ArticleTypeServiceImpl;
import com.hl.springboothome1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("articleType")
@CrossOrigin //允许跨域访问
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService  articleTypeService = new ArticleTypeServiceImpl();

    @PostMapping
    public R add(@RequestBody ArticleType articleType){
        int i = articleTypeService.add(articleType);
        return R.ok("添加成功"+i);
    }
    //id删除
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") int id){
        int i = articleTypeService.deleteById(id);
        return R.ok("删除"+i);
    }

    //批量删除
    @RequestMapping("deleteBatch")
    public R deleteBatch(@RequestParam("list") List list){
        int i = articleTypeService.deleteBatch(list);
        return R.ok("批量删除成功！！");
    }

    //更新
    @PutMapping
    public R update(@RequestBody ArticleType articleType){
        int update = articleTypeService.update(articleType);
        return R.ok("修改成功！！");
    }

    //查询
    @GetMapping("{id}")
    public R selectById(@PathVariable("id") int id){
        ArticleType article = articleTypeService.selectById(id);
        return R.ok(article.toString());
    }
    //分页动态查询
    @GetMapping("selectAll")
    public R selectAll(ArticleType articleType,
                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "3") int pageSize){
        PageInfo<ArticleType> articleTypePageInfo = articleTypeService.selectAll(articleType, pageNum, pageSize);
        return R.ok(articleTypePageInfo);
    }
}
